define([
    "app"
], function (app) {
    app.controller("createNewApplicationRoundDialogController", [
        "$scope",
        "$uibModalInstance",
        "dateService",
        "indexDataService",
        "$state",
        function ($scope, $uibModalInstance,dateService,indexDataService,$state) {

            $scope.applicationRound = {
                beginDate:new Date(),
                endDate:new Date(),
                termId:dateService.format(new Date(),"YYYYMM")
            };

            $scope.test = function (obj) {
              console.log(obj);
            };

            $scope.popup = {
                isOpen1: false,
                isOpen2: false
            };

            $scope.dateOptions = {
                minDate:new Date(),
                dateDisabled:false
            };

            // 关闭当前窗口
            $scope.close = function () {
                $uibModalInstance.close();
            };

            // 打开申请开始日期的选择器控件
            $scope.openSelector1 = function () {
                $scope.popup.isOpen1 = true;
            };

            // 打开申请结束日期的选择器控件
            $scope.openSelector2 = function () {
                $scope.popup.isOpen2 = true;
            };

            // 比较两个日期
            $scope.compareDate = dateService.compare;

            // 提交新一期的申请
            $scope.submitNewApplication = function () {
                if(confirm("确定要发布吗?")) {
                    indexDataService
                        .createNewApplicationRound($scope.applicationRound)
                        .then(
                            function success(resp) {
                                alert("发布成功!");
                                // 关闭窗口
                                $uibModalInstance.dismiss();
                                // 刷新页面
                                $state.go($state.current,{},{reload:true});
                            },
                            function error(resp) {
                                alert("似乎出了点问题!!!∑(ﾟДﾟノ)ノ");
                                // 关闭窗口
                                $uibModalInstance.dismiss();
                                // 刷新页面
                                $state.go($state.current,{},{reload:true});
                            }
                        )
                }
            };

        }])
});